package com.hx.er.action.manager.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErManagerAction;
import com.hx.er.bean.base.LanguageBean;
import com.hx.er.bean.base.UserBean;
import com.hx.er.common.system.Constants;
import com.hx.er.common.system.ErLanguages;
import com.hx.er.common.util.MD5Util;
import com.hx.er.common.util.StringUtil;
import com.hx.er.service.manager.IManagerUserService;

public class ManagerPasswordEditAction extends ErManagerAction {
	private static final long serialVersionUID = 4926512300151605899L;

	private UserBean userBeanPage;
	private String confirmPassword;
	private String oldPassword;
	private List<Integer> ageList;
	private List<LanguageBean> langList;
	private Map<String, String> sexMap;

	@Autowired
	private IManagerUserService managerInfoEditService;

	public String init() {
		// 页面保存用户ID
		this.userBeanPage = new UserBean();
		this.userBeanPage.setUserId(super.userInfoLogin.getUserId());
		return SUCCESS;
	}

	/**
	 * 保存密码修改
	 * 
	 * @return
	 */
	public String save() {
		// 校验
		if (!this.checkParams()) {
			this.initInputPage();
			return INPUT;
		}

		// 定义参数map
		Map<String, Object> paramsMap = new HashMap<String, Object>();

		// 密码转换为MD5
		paramsMap.put("oldPassword", MD5Util.encodeMD5(oldPassword));
		paramsMap.put("newPassword", MD5Util.encodeMD5(confirmPassword));

		// 设置其他参数
		paramsMap.put("userId", this.userBeanPage.getUserId());

		// 设定用户类型
		paramsMap.put("userTypeId", Constants.USER_TYPE_MANAGER);

		// 更新密码，因为后台sql为动态，所以只要password属性不为空，就会自动更新密码
		int count = this.managerInfoEditService.updateUserPassword(paramsMap);

		if (count == 0) {
			// 显示错误信息
			this.addFieldError("msg.oldPassword", "原密码输入错误！");
			// 初始化
			this.initInputPage();
			// 页面跳转
			return INPUT;
		} else {
			// 显示成功信息
			this.addActionMessage("更新成功！");
			// 页面跳转
			return SUCCESS;
		}
	}

	/**
	 * 页面验证
	 */
	private boolean checkParams() {
		// 密码验证
		if (StringUtil.isEmpty(this.userBeanPage.getPassword())) {
			this.addFieldError("msg.newPassword", "密码不能为空!");
		} else if (!StringUtil.checkLength(this.userBeanPage.getPassword(), 6, 16)) {
			this.addFieldError("msg.newPassword", "密码长度只能在6-16位之间!");
		}

		// 重复密码验证
		if (StringUtil.isEmpty(this.confirmPassword)) {
			this.addFieldError("msg.confirmPassword", "密码确认不能为空!");
		} else if (!StringUtil.isEqual(this.confirmPassword, this.userBeanPage.getPassword())) {
			this.addFieldError("msg.confirmPassword", "两次输入的密码必须相同!");
		}

		// 错误信息数量
		int errCount = this.getActionErrors().size() + this.getFieldErrors().size();

		return errCount == 0;
	}

	/**
	 * 初始化页面需要的下拉框等元素
	 */
	private void initInputPage() {
		// 初始化年龄，范围为1～100
		this.ageList = new ArrayList<Integer>();
		for (int i = 10; i < 100; i++) {
			this.ageList.add(i + 1);
		}

		// 初始化语言
		this.langList = ErLanguages.getLangList();

		// 初始化性别
		this.sexMap = new HashMap<String, String>();
		this.sexMap.put("0", "女");
		this.sexMap.put("1", "男");
	}

	public UserBean getUserBeanPage() {
		return userBeanPage;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public List<Integer> getAgeList() {
		return ageList;
	}

	public void setAgeList(List<Integer> ageList) {
		this.ageList = ageList;
	}

	public List<LanguageBean> getLangList() {
		return langList;
	}

	public void setLangList(List<LanguageBean> langList) {
		this.langList = langList;
	}

	public Map<String, String> getSexMap() {
		return sexMap;
	}

	public void setSexMap(Map<String, String> sexMap) {
		this.sexMap = sexMap;
	}

	public void setUserBeanPage(UserBean userBeanPage) {
		this.userBeanPage = userBeanPage;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
}
